import 'package:flutter/material.dart';

class ServiceTimePage extends StatefulWidget {
  const ServiceTimePage({super.key});

  @override
  State<ServiceTimePage> createState() => _ServiceTimePageState();
}

class _ServiceTimePageState extends State<ServiceTimePage> {
  // 服务时间设置
  final Map<String, Map<String, dynamic>> _timeSettings = {
    '工作日': {
      'enabled': true,
      'startTime': const TimeOfDay(hour: 9, minute: 0),
      'endTime': const TimeOfDay(hour: 18, minute: 0),
      'autoReceive': true,
    },
    '周末': {
      'enabled': false,
      'startTime': const TimeOfDay(hour: 10, minute: 0),
      'endTime': const TimeOfDay(hour: 17, minute: 0),
      'autoReceive': false,
    },
    '节假日': {
      'enabled': false,
      'startTime': const TimeOfDay(hour: 10, minute: 0),
      'endTime': const TimeOfDay(hour: 17, minute: 0),
      'autoReceive': false,
    },
  };

  Future<void> _selectTime(String day, bool isStart) async {
    final TimeOfDay? picked = await showTimePicker(
      context: context,
      initialTime: isStart ? _timeSettings[day]!['startTime'] : _timeSettings[day]!['endTime'],
      builder: (context, child) {
        return Theme(
          data: Theme.of(context).copyWith(
            colorScheme: const ColorScheme.light(
              primary: Color(0xFFFF5722),
              onPrimary: Colors.white,
            ),
          ),
          child: child!,
        );
      },
    );
    if (picked != null && mounted) {
      setState(() {
        if (isStart) {
          _timeSettings[day]!['startTime'] = picked;
        } else {
          _timeSettings[day]!['endTime'] = picked;
        }
      });
    }
  }

  void _toggleEnabled(String day, bool value) {
    setState(() {
      _timeSettings[day]!['enabled'] = value;
    });
  }

  void _toggleAutoReceive(String day, bool value) {
    setState(() {
      _timeSettings[day]!['autoReceive'] = value;
    });
  }

  String _formatTime(TimeOfDay time) {
    final hour = time.hour.toString().padLeft(2, '0');
    final minute = time.minute.toString().padLeft(2, '0');
    return '$hour:$minute';
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFFF5F5F5),
      appBar: AppBar(
        title: const Text('服务时间'),
        centerTitle: true,
        backgroundColor: Colors.white,
        elevation: 0,
      ),
      body: ListView(
        padding: const EdgeInsets.all(16),
        children: [
          ..._timeSettings.entries.map((entry) => Column(
            children: [
              Container(
                padding: const EdgeInsets.all(16),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(8),
                ),
                child: Column(
                  children: [
                    Row(
                      children: [
                        Text(
                          entry.key,
                          style: const TextStyle(
                            fontSize: 16,
                            fontWeight: FontWeight.w500,
                          ),
                        ),
                        const Spacer(),
                        Switch.adaptive(
                          value: entry.value['enabled'],
                          onChanged: (value) => _toggleEnabled(entry.key, value),
                          activeColor: const Color(0xFFFF5722),
                        ),
                      ],
                    ),
                    if (entry.value['enabled']) ...[
                      const Divider(height: 24),
                      Row(
                        children: [
                          const Text(
                            '服务时段',
                            style: TextStyle(
                              fontSize: 14,
                              color: Colors.black87,
                            ),
                          ),
                          const Spacer(),
                          GestureDetector(
                            onTap: () => _selectTime(entry.key, true),
                            child: Text(
                              _formatTime(entry.value['startTime']),
                              style: const TextStyle(
                                fontSize: 15,
                                fontWeight: FontWeight.w500,
                              ),
                            ),
                          ),
                          const Padding(
                            padding: EdgeInsets.symmetric(horizontal: 8),
                            child: Text(
                              '至',
                              style: TextStyle(
                                fontSize: 14,
                                color: Colors.black54,
                              ),
                            ),
                          ),
                          GestureDetector(
                            onTap: () => _selectTime(entry.key, false),
                            child: Text(
                              _formatTime(entry.value['endTime']),
                              style: const TextStyle(
                                fontSize: 15,
                                fontWeight: FontWeight.w500,
                              ),
                            ),
                          ),
                        ],
                      ),
                      const SizedBox(height: 16),
                      Row(
                        children: [
                          const Text(
                            '自动接单',
                            style: TextStyle(
                              fontSize: 14,
                              color: Colors.black87,
                            ),
                          ),
                          const Spacer(),
                          Switch.adaptive(
                            value: entry.value['autoReceive'],
                            onChanged: (value) => _toggleAutoReceive(entry.key, value),
                            activeColor: const Color(0xFFFF5722),
                          ),
                        ],
                      ),
                    ],
                  ],
                ),
              ),
              const SizedBox(height: 12),
            ],
          )).toList(),
          
          Container(
            padding: const EdgeInsets.all(12),
            decoration: BoxDecoration(
              color: const Color(0xFFFFF3E0),
              borderRadius: BorderRadius.circular(8),
            ),
            child: Row(
              children: [
                const Icon(
                  Icons.info_outline,
                  color: Color(0xFFFF5722),
                  size: 20,
                ),
                const SizedBox(width: 8),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        '开启后将在对应时段接收订单推送',
                        style: TextStyle(
                          fontSize: 13,
                          color: Colors.grey[700],
                        ),
                      ),
                      const SizedBox(height: 4),
                      Text(
                        '开启自动接单后，系统将自动为您接受符合条件的订单',
                        style: TextStyle(
                          fontSize: 13,
                          color: Colors.grey[700],
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
} 